package com.aft.terra.modules.game.model;

import com.aft.terra.modules.game.model.base.BaseUser;
import com.jfinal.plugin.activerecord.Db;

public class User extends BaseUser<User> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public static final User dao = new User();

	public User queryUser(String userId){
		String sql ="select u.*,(select w_headimgurl from t_wechat where id = u.d_wechat_id ) w_headimgurl from t_user u where id = ?";
		return findFirst(sql,userId);
	}
	
	public User queryWechatUser(String wechatId){
		String sql ="select * from t_user where d_wechat_id = ?";
		return findFirst(sql,wechatId);
	}
	
	public boolean findByPhone(String phone) {
		String sql ="select * from t_user where d_phone = ?";
		return find(sql,phone).size() > 0;
	}
	
	/**
	 * 用户减游戏次数
	 * @param userId
	 * @param fu
	 * @param count
	 * @return
	 */
	public Boolean updateSubUserGameCount(String userId ,Integer count ){
		StringBuffer sql = new StringBuffer()
				.append("UPDATE t_user SET d_play_game_frequency = d_play_game_frequency ")
				.append("-")
				.append(count)
				.append(" WHERE id = ?");
		return Db.update(sql.toString(),userId) > 0;
	}
	/**
	 * 用户新增次数
	 * @param userId
	 * @param fu
	 * @param count
	 * @return
	 */
	public Boolean updateUserGameCount(String userId,Integer count ){
		StringBuffer sql = new StringBuffer()
				.append("UPDATE t_user SET d_game_frequency_sum = d_game_frequency_sum + ? ,  d_play_game_frequency = d_play_game_frequency ")
				.append("+")
				.append(" ? ")
				.append(" WHERE id = ?");
		return Db.update(sql.toString(),count,count,userId) > 0;
	}
}
